#Replication File
#The Determinants of Insurgent Gender Governance
#Conditionally accepted at International Organization
#Tessa Devereaux 

#Load packages 

library(tidyverse)
library(haven)
library(magrittr)
library(stargazer)
library(labelled)
library(readr)
library(dplyr)
library(forcats)
library(haven)
library(margins)
library(performance)
library(gtsummary)
library(openxlsx)
library(broom)
library(sjPlot)
library(modelsummary)
library(ggplot2)
library(gt)
library(officer)
library(flextable)
library(tidyr)
library(readxl)

#Load data & name data as IOdatafinal


IOdatafinal <- read_excel("~/Library/CloudStorage/OneDrive-Personal/IO To have and to Hold resubmission August 2024/IOdataDec16th2024.xlsx")

attach(IOdatafinal)


# FIGURE 3: GENDER GOVERNANCE STRATEGIES

df2 <- data.frame(
  "Gender Governance Total" = as.numeric(as.character(IOdatafinal$gendergovall)),
  "Change Gender Status Quo" = as.numeric(as.character(IOdatafinal$changegenderstatusquo)),
  "Change bride price or dowry customs" = as.numeric(as.character(IOdatafinal$bridepricedowry)),
  "Force Civilians into Marriage" = as.numeric(as.character(IOdatafinal$forcedmarriage)),
  "Ban Adultery" = as.numeric(as.character(IOdatafinal$adulteryextramaritalban)),
  "Civilian women in local governance" = as.numeric(as.character(IOdatafinal$civwomenpubliclife)),
  "Dress Code for women" = as.numeric(as.character(IOdatafinal$dresscode)),
  "Restrict female freedom of movement" = as.numeric(as.character(IOdatafinal$restrictwomenfreedom)),
  "Adjudicate Civilian Marriages" = as.numeric(as.character(IOdatafinal$organizemarriageceremonies)),
  "Punish Domestic Violence" = as.numeric(as.character(IOdatafinal$domesticviolence)),
  "Marry local women as official strategy" = as.numeric(as.character(IOdatafinal$marrylocalstrategy)),
  "Restrict Wedding Celebrations" = as.numeric(as.character(IOdatafinal$restrictweddingceremonies)),
  "Finance Wedding Costs" = as.numeric(as.character(IOdatafinal$financeweddingcosts)),
  "Allow marriage without parental consent" = as.numeric(as.character(IOdatafinal$parentalconsentformarriageoppose)),
  "Limit wedding costs" = as.numeric(as.character(IOdatafinal$limitweddingcosts)),
  "Divorce rights for women" = as.numeric(as.character(IOdatafinal$improvewomendivorcerights)),
  "Keep civilian marriage records" = as.numeric(as.character(IOdatafinal$keepmarriagerecords)),
  "Female consent in marriage enforce" = as.numeric(as.character(IOdatafinal$femalemarriageconsentenforce)),
  "Distribute land to women" = as.numeric(as.character(IOdatafinal$landdistrib)),
  "Enforce minimum age for marriage" = as.numeric(as.character(IOdatafinal$minimummarriageageenforce)),
  "Ban Polygamy" = as.numeric(as.character(IOdatafinal$polygamyoppose))
)

percentage_data <- colMeans(df2) * 100

percentage_df <- data.frame(
  Variable = names(percentage_data),
  Percentage = percentage_data
)

# Order percentages by size 
percentage_df$Variable <- reorder(percentage_df$Variable, percentage_df$Percentage)

# Define the new labels for each variable
new_labels <- c("Gender Governance Total", "Change Gender Status Quo", "Change bride price or dowry customs", "Force Civilians into Marriage", 
                "Ban Adultery", "Civilian women in local governance", "Dress Code for Women", "Restrict female freedom of movement", 
                "Adjudicate Civilian Marriages", "Punish Domestic Violence", "Marry local women as official strategy", "Restrict Wedding Celebrations",
                "Finance Wedding Costs", "Allow marriage without parental consent", "Limit wedding costs", 
                "Divorce rights for women", "Keep civilian marriage records", "Female consent in marriage enforce", 
                "Distribute land to women", "Enforce minimum age for marriage", "Ban Polygamy")


# Reverse the order of the labels
new_labels_reversed <- rev(new_labels)

new_labels_reversed


# Ensure that the length of new_labels matches the number of binary variables (21 in this case)
if (length(new_labels) != length(percentage_df$Variable)) {
  stop("The number of new labels does not match the number of variables.")
}

# Map the new labels to the variables
percentage_df$Variable <- factor(percentage_df$Variable, labels = new_labels_reversed)

# FIGURE 3 BAR CHART - Gender Governance Strategies 

ggplot(percentage_df, aes(x = Variable, y = Percentage)) +
  geom_bar(stat = "identity", fill = "skyblue") +
  labs(title = "", 
       x = "", 
       y = "Percentage (%)") +
  theme(
    axis.text.x = element_text(hjust = 1),  
    panel.background = element_blank(),  
    plot.background = element_blank(),  
    panel.grid.major.y = element_blank(), 
    panel.grid.minor.y = element_blank(),  
    panel.grid.major.x = element_line(color = "gray", size = 0.2),  
    panel.grid.minor.x = element_line(color = "gray", size = 0.2)   
  ) +
  coord_flip()  # Flip the axes


# FIGURE 4: GENDER GOVERNANCE EXTENT Descriptives

ggplot(IOdatafinal, aes(x = gendergovextent)) +
  geom_histogram(fill = "lightblue", color = "black", alpha = 0.7) +
  labs(x = "Gender Governance Extent", y = "Frequency") +
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5))


# TABLE 2: APPROACH TO LOCAL ELITES Descriptives 

df <- data.frame(
  Var1 = as.numeric(as.character(IOdatafinal$purechallenge)),
  Var2 = as.numeric(as.character(IOdatafinal$partialchallenge)),
  Var3 = as.numeric(as.character(IOdatafinal$cooperate)),
  Var4 = as.numeric(as.character(IOdatafinal$nocontact))
)

distribution <- data.frame(
  Approach = c("purechallenge", "partialchallenge", "cooperate", "nocontact"),
  Count = colSums(df),  # Sum of each column (number of 1s in each variable)
  Percentage = (colSums(df) / nrow(df)) * 100  # Calculate percentage
)


# Round the Percentage column to 1 decimal place
distribution$Percentage <- round(distribution$Percentage, 1)


# Code for Table 2: Approach to Local Elites 

print(distribution)
distribution$Approach <- c("Pure Challenge", "Partial Challenge", "Cooperate", "No Contact")
ft <- flextable(distribution)
ft



# Creating decade dummies 

start_decade <- floor(startyear1 / 10) * 10
end_decade <- floor(endyear1 / 10) * 10

start_decade <- as.numeric(start_decade)
end_decade <- as.numeric(end_decade)

min_decade <- min(start_decade, end_decade)
max_decade <- max(start_decade, end_decade)

decades <- seq(min_decade, max_decade, by = 10)

decade_dummies <- matrix(0, nrow = length(startyear1), ncol = length(decades))

for (i in 1:length(startyear1)) {
  start_index <- which(decades == start_decade[i])
  end_index <- which(decades == end_decade[i])
  decade_dummies[i, start_index:end_index] <- 1
}

colnames(decade_dummies) <- decades


#FIGURE 5 Models: Determinants of Change Gender Status Quo

gendergovmodel1 <- lm(changegenderstatusquo ~ challengecombined + nocontact + leftist + 
                              islamist + secessionist + femaleparticipants + terrcont + foreignsupport + rebelstrength + decade_dummies + fixed_effect_dummy, data = IOdatafinal)
summary(gendergovmodel1)

gendergovmodel2 <- lm(changegenderstatusquo ~ purechallenge + partialchallenge + nocontact + leftist + 
                              islamist + secessionist + femaleparticipants + terrcont + foreignsupport + rebelstrength + decade_dummies + fixed_effect_dummy, data = IOdatafinal)
summary(gendergovmodel2)

#FIGURE 6 Models: Determinants of Insurgent Gender Governance Extent 

gendergovextentmodel1 <- lm(gendergovextent ~ challengecombined + nocontact + leftist + 
                              islamist + secessionist + femaleparticipants + terrcont + foreignsupport + rebelstrength + decade_dummies + fixed_effect_dummy, data = IOdatafinal)

summary(gendergovextentmodel1)

gendergovextentmodel2 <- lm(gendergovextent ~ purechallenge + partialchallenge + nocontact + leftist + 
                              islamist + secessionist + femaleparticipants + terrcont + foreignsupport + rebelstrength + decade_dummies + fixed_effect_dummy, data = IOdatafinal)

summary(gendergovextentmodel2)


#FIGURE 5 Chart: Determinants of Change Gender Status Quo 

cm <- c('challengecombined1' = 'Challenge Combined',
        'purechallenge1' = 'Pure Challenge',
        'partialchallenge1' = 'Partial Challenge',
        'cooperate' = 'Cooperate',
        'statusquo' = 'Status Quo',
        'nocontact' = 'No Contact',
        'leftist1' = 'Leftist',
        'islamist' = 'Islamist',
        'secessionist1' = 'Secessionist',
        'femaleparticipants' = 'Female Participants',
        'terrcont' = 'Territorial Control',
        'rebelstrength' = 'Rebel Strength',
        'foreignsupport' = 'Foreign Support')


gendergovmodels <- list(
  "Model (1)" = gendergovmodel1,
  "Model (2)" = gendergovmodel2)

modelplot(models = gendergovmodels, color="black",
          coef_map = rev(cm)) + geom_vline(xintercept = 0, color ="red", linetype = "dashed", linewidth = .75) + facet_grid(~model)


#FIGURE 6 Chart: Determinants of Insurgent Gender Governance Extent 

govextentmodels <- list(
  "Model (3)" = gendergovextentmodel1,
  "Model (4)" = gendergovextentmodel2)

modelplot(models = govextentmodels, color="black",
          coef_map = rev(cm)) + geom_vline(xintercept = 0, color ="red", linetype = "dashed", linewidth = .75) + facet_grid(~model)


### APPENDIX MODELS 

logitgendergov1 <- glm(changegenderstatusquo ~ challengecombined + nocontact + leftist + 
                         islamist + secessionist + femaleparticipants + terrcont + foreignsupport + rebelstrength + decade_dummies + fixed_effect_dummy, data = IOdatafinal, family = "binomial")
summary(logitgendergov1)

logitgendergov2 <- glm(changegenderstatusquo ~ purechallenge + partialchallenge + nocontact + leftist + 
                         islamist + secessionist + femaleparticipants + terrcont + foreignsupport + rebelstrength + decade_dummies + fixed_effect_dummy, data = IOdatafinal, family = "binomial")
summary(logitgendergov2)

statusquomodel <- lm(statusquo ~ cooperate + nocontact + leftist + 
                       islamist + secessionist + femaleparticipants + terrcont + foreignsupport + rebelstrength + decade_dummies + fixed_effect_dummy, data = IOdatafinal)
summary(statusquomodel)

subsetmodel1 <- lm(changegenderstatusquo ~ challengecombinesubset + leftist + 
                     islamist + secessionist + femaleparticipants + terrcont + foreignsupport + rebelstrength + decade_dummies + fixed_effect_dummy, data = IOdatafinal)
summary(subsetmodel1)

subsetmodel2 <- lm(changegenderstatusquo ~ purechallengesubset + partialchallengesubset + leftist + 
                     islamist + secessionist + femaleparticipants + terrcont + foreignsupport + rebelstrength + decade_dummies + fixed_effect_dummy, data = IOdatafinal)
summary(subsetmodel2)

subsetmodel3 <- lm(changegenderstatusquo ~ cooperatesubset + leftist + 
                     islamist + secessionist + femaleparticipants + terrcont + foreignsupport + rebelstrength + decade_dummies + fixed_effect_dummy, data = IOdatafinal)
summary(subsetmodel3)

### Formatting for stargazer tables 

fixed_effect_vars <- c("decade_dummies1950", "decade_dummies1960", "decade_dummies1970", 
                "decade_dummies1980", "decade_dummies1990", "decade_dummies2000", 
                "decade_dummies2010", "fixed_effect_dummy433", "fixed_effect_dummy436", 
                "fixed_effect_dummy437", "fixed_effect_dummy450", "fixed_effect_dummy451", 
                "fixed_effect_dummy490", "fixed_effect_dummy500", "fixed_effect_dummy522", 
                "fixed_effect_dummy530", "fixed_effect_dummy531", "fixed_effect_dummy540", 
                "fixed_effect_dummy541", "fixed_effect_dummy560", "fixed_effect_dummy600", 
                "fixed_effect_dummy615", "fixed_effect_dummy625", "fixed_effect_dummy626", 
                "decade_dummies21950", "decade_dummies21960", "decade_dummies21970", "decade_dummies21980", "decade_dummies21990", "decade_dummies22000","decade_dummies22010",
                "decade_dummies31950", "decade_dummies31960", "decade_dummies31970", "decade_dummies31980", "decade_dummies31990", "decade_dummies32000","decade_dummies32010")


### APPENDIX TABLE 1: Determinants of Gender Governance Extent

stargazer(gendergovmodel1, gendergovmodel2,
          type="html",
          omit=fixed_effect_vars,
          covariate.labels = c("Challenge Combined", "Pure Challenge", "Partial Challenge","No Contact", "Leftist", "Islamist", "Secessionist", "Female Participants", "Territorial Control", "Foreign Support", "Rebel Strength"),
          out="gendergovmodels.doc")


### APPENDIX TABLE 2: Determinants of Change Gender Status Quo

stargazer(gendergovextentmodel1, gendergovextentmodel2,
          type="html",
          omit=fixed_effect_vars,
          covariate.labels = c("Challenge Combined", "Pure Challenge", "Partial Challenge","No Contact", "Leftist", "Islamist", "Secessionist", "Female Participants", "Territorial Control", "Foreign Support", "Rebel Strength"),
          out="genderextentmodels.doc")


### APPENDIX TABLE 3: Logit Plots 

stargazer(logitgendergov1, logitgendergov2,
          type="html",
          omit=fixed_effect_vars,
          covariate.labels = c("Challenge Combined", "Pure Challenge", "Partial Challenge","No Contact", "Leftist", "Islamist", "Secessionist", "Female Participants", "Territorial Control", "Foreign Support", "Rebel Strength"),
          out="logitmodels.doc")


### APPENDIX TABLE 4: Status Quo Plots 

stargazer(statusquomodel,
          type="html",
          omit=fixed_effect_vars,
          covariate.labels = c("Cooperate","No Contact", "Leftist", "Islamist", "Secessionist", "Female Participants", "Territorial Control", "Foreign Support", "Rebel Strength"),
          out="statusquomodel.doc")


### APPENDIX TABLE 5: Removing No Contact groups

stargazer(subsetmodel1, subsetmodel2, subsetmodel3,
          type="html",
          omit=fixed_effect_vars,
          covariate.labels = c("Challenge Combined", "Pure Challenge", "Partial Challenge", "Cooperate", "Leftist", "Islamist", "Secessionist", "Female Participants", "Territorial Control", "Foreign Support", "Rebel Strength"),
          out="subsetmodels.doc")









